package com.jixi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jixi.common.result.Result;
import com.jixi.pojo.entity.Setmeal;
import com.jixi.pojo.vo.SetmealAndOrderInfoVO;
import com.jixi.service.OrderDailyService;
import com.jixi.service.SetmealService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/education")
public class SetmealController {
	
	@Autowired
	private SetmealService setmealService;
	
	@Autowired
	private OrderDailyService orderDailyService;
	
	/**
	 * 套餐条件列表
	 *
	 * @param pageNum     页码
	 * @param pageSize    每页数量
	 * @param setmealName 套餐名称
	 * @param companyId   所属餐饮公司id
	 * @param category    套餐类型
	 * @param auditStatus 审核状态
	 * @param status      状态
	 * @return 套餐列表
	 */
	@GetMapping("/setmeal/list")
	public Result<Page<SetmealAndOrderInfoVO>> setmealList(
			Integer pageNum,
			Integer pageSize,
			String setmealName,
			Long companyId,
			String category,
			Integer auditStatus,
			Integer status
	) {
		return setmealService.setmealPage(pageNum,pageSize,setmealName,companyId,category,auditStatus,status);
	}
	
	/**
	 * 智能体调用api测试接口
	 *
	 * 参数同上setmealList方法
	 */
	@GetMapping("/Agent_TEST_getSetmealList")
	public Result<Page<SetmealAndOrderInfoVO>> setmealList_Agent_TEST(
			Integer pageNum,
			Integer pageSize,
			String setmealName,
			Long companyId,
			String category,
			Integer auditStatus,
			Integer status
	) {
		return setmealService.setmealPage(pageNum,pageSize,setmealName,companyId,category,auditStatus,status);
	}
	
	/**
	 * 通过套餐id查询套餐信息
	 *
	 * @param setmealId 套餐id
	 * @return 套餐信息
	 */
	@GetMapping("/setmeal/info")
	public Result<Setmeal> getInfo(Long setmealId) {
		Setmeal setmeal = setmealService.getSetMealInfoById(setmealId);
		return Result.success(setmeal);
	}
	
	/**
	 * 审核套餐
	 *
	 * @param setmealId      套餐id
	 * @param auditStatus    审核状态
	 * @param examineRemarks 审核备注
	 * @return 审核结果
	 */
	@PutMapping("/setmeal/examine")
	public Result updateStatus(Long setmealId, Integer auditStatus, String examineRemarks) {
		return setmealService.updateSetmealExamine(setmealId,auditStatus,examineRemarks);
	}
	
	/**
	 * 查询套餐的已完成订单数量和总金额
	 * <!>
	 * 该接口可能会废弃，相关功能已在套餐条件列表分页查询中实现
	 * <!>
	 *
	 * @param setmealId 套餐id
	 * @return 查询结果
	 */
	@GetMapping("/setmeal/numAndMoney")
	public Result<Map<String, Object>> countSetmealNumAndMoney(Long setmealId) {
		return orderDailyService.selectsetmealumAndMoney(setmealId);
	}
	
	/**
	 * 查询公司与学校相关联的套餐的订单总数量和总金额
	 *
	 * @param companyId 公司id
	 * @param schoolId  学校id
	 * @param timeValue 时间（天）
	 * @return 查询结果
	 */
	@GetMapping("/count/company/school/setmeal")
	public Result<Map<String, Object>> countCompanySchoolSetmeal(
			Long companyId,
			Long schoolId,
			String timeValue
	) {
		return orderDailyService.selectCountCompanySchoolSetmeal(companyId,schoolId,timeValue);
	}
	
	/**
	 * 1导出公司与学校相关联的套餐的订单详情excel
	 */
	@GetMapping("/count/company/school/setmeal/download")
	@Operation(summary = "导出公司与学校相关联的套餐的订单详情")
	public void downloadOrderToday(HttpServletResponse response, String timeValue) throws IOException {
		setmealService.downloadCountCompanySchoolSetmeal(response, timeValue);
	}
	
	/**
	 * 4导出各套餐统计详细excel
	 */
	@GetMapping("/setmeal/list/download")
	@Operation(summary = "导出公司与学校相关联的套餐的订单详情")
	public void downloadSetMealList(HttpServletResponse response) throws IOException {
		setmealService.downloadSetmeal(response);
	}
}
